<?php
/*******************Ajax get list discounts for type discounts*****************************************/
/******************************************************************************************************/
function GetListDiscounts($type)
{
    $core = getCoreInstance();
    $db = $core->getProperty("db");
    $arr_items = $db->getAll("select id_discount, title from discount where id_type = {$type} order by title asc");
    
    $ret = "";
    foreach ($arr_items as $item)
    {
        $ret .= ($ret == "" ? "" : "~~") . $item['id_discount'] . "|" . db2str($item['title']);
    }

    return $ret;
}


/*******************Ajax get list products for discounts***********************************************/
/******************************************************************************************************/
function GetListProducts($catalog)
{
    $core = getCoreInstance();
    $db = $core->getProperty("db");

    $str_catalogid = $core->config['discounts']['catalogid'];
    $str_parent_id = $core->config['discounts']['parent_id'];
    $str_title = $core->config['discounts']['title'];
    $str_table = $core->config['discounts']['product_table'];
    $str_iscategory = $core->config['discounts']['iscategory'];
    $str_iscategory_true = $core->config['discounts']['iscategoryTrue'];
    $str_iscategory_false = $core->config['discounts']['iscategoryFalse'];
    $str_itemnumber = $core->config['discounts']['itemnumber'];

    // Get list products in root from table with products
    $parent_id = ("" == $str_parent_id) ? "" : " and " . $str_parent_id . " = " . $catalog;
    $iscategory = ("" == $str_iscategory) ? "" : " and " . $str_iscategory . " = '{$str_iscategory_false}'";

    $sql = "select " . ("" == $str_itemnumber ? "" : $str_itemnumber . ", ");
    $sql .= "{$str_catalogid} as catalogid, {$str_title} as title from {$str_table} where 1 = 1 {$iscategory}";
    $sql .= $parent_id . " order by title asc";
    $arr_items = $db->getAll($sql, 2);
    
    $ret = "";
    foreach ($arr_items as $item)
    {
        $ret .= ($ret == "" ? "" : "~~") . $item['catalogid'] . "|" . db2str($item[0]) . "-" . db2str($item['title']);
    }

    return $ret;
}

?>